// pages/weijieke/industry/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    industryCode: 0,
    cardInfo: { IndustryCode: 0 },
    classList: [],
    count: 0
  },

  changeIndustryCode: function (e) {
    let code = this.data.cardInfo.IndustryCode;
    let industryCode = e.currentTarget.dataset.code;
    if (code > 0) {
      industryCode = code;
    }
    this.setData({
      industryCode: industryCode
    })
  },
  changeStoreiid: function (e) {
    let classList = this.data.classList;
    let storeiid = e.currentTarget.dataset.storeiid;
    let count = this.data.count;
    for (let i = 0; i < classList.length; i++) {
      if (storeiid == classList[i].StoreIid) {
        if (classList[i].isSelect == 1) {
          classList[i].isSelect = 0;
          this.data.count -= 1;
        } else {
          if (count > 2) {
            wx.showToast({
              icon: 'none',
              title: '分类至多可选三个'
            })
            return;
          } else {
            classList[i].isSelect = 1;
            this.data.count += 1;
          }
        }
      }
    }
    this.setData({
      classList: classList
    })
  },
  joinIndustry: function () {
    let that = this;
    let industryCode = that.data.industryCode;
    if (!industryCode) {
      wx.showModal({
        title: '提示',
        content: '请选择要加入的行业',
        showCancel: false
      })
    } else {
      if (that.data.cardInfo.IndustryCode >= 1) {
        that.setStaffIndustyCode(industryCode);
      } else {
        wx.showModal({
          title: '提示',
          content: '选择门店商家后，此版本按199元/年，并无法撤销，是否确认选择？',
          success(res) {
            if (res.confirm) {
              that.setStaffIndustyCode(industryCode);
            }
          }
        })
      }
    }
  },
  // 设置行业标签
  setStaffIndustyCode: function (industryCode) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Staff/SetStaffIndustyCode',
      data: {
        uid: app.globalData.userInfo.Uid,
        code: industryCode
      },
      method: 'POST',
      success: function (res) {
        that.setIndustry();
        if (res.data.Code == 0) {

        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },
  // 设置行业分类
  setIndustry: function () {
    let that = this;
    let cardId = wx.getStorageSync("cardId");
    let classList = that.data.classList;
    let industryList = '';
    for (let i = 0; i < classList.length; i++) {
      if (classList[i].isSelect == 1) {
        industryList += classList[i].StoreIid + ',';
      }
    }
    if (industryList == '') {
      wx.showModal({
        title: '提示',
        content: '请选择行业分类',
        showCancel: false
      })
      return;
    }
    if (cardId) {
      wx.request({
        url: app.globalData.host + '/api/Staff/SetIndustry?staffId=' + cardId + '&content=' + industryList,
        success: function (res) {
          if (res.data.Code == 0) {
            wx.showToast({
              title: '保存成功',
              mask: true
            })
            wx.reLaunch({
              url: '/pages/index/index?id=' + cardId
            })
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.Message,
              showCancel: false
            })
          }
        }
      })
    }
  },

  // 判断是否是共享版
  getAiCardInfo: function () {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/Staff/GetStaffIdByUid?Uid=' + app.globalData.userInfo.Uid,
      success: function (res) {
        if (res.data.Code == 0) {
          let cardInfo = {};
          cardInfo.IndustryCode = res.data.Data.IndustryCode;
          that.data.cardInfo = cardInfo;
          that.setData({
            industryCode: res.data.Data.IndustryCode,
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },
  // 获取名片所选分类
  getStaffExtend: function (classList) {
    let that = this;
    let cardId = wx.getStorageSync("cardId");
    if (cardId) {
      wx.request({
        url: app.globalData.host + '/api/Staff/GetStaffExtend?staffId=' + cardId + '&keyword=24',
        success: function (res) {
          if (res.data.Code == 0) {
            if (res.data.Data) {
              let values = res.data.Data.Value;
              if (values) {
                let list = values.split(",");
                let count = list.length;
                if (list[count - 1] == '') {
                  count -= 1;
                }
                that.data.count = count;
                for (let i = 0; i < list.length; i++) {
                  for (let j = 0; j < classList.length; j++) {
                    if (list[i] == classList[j].StoreIid) {
                      classList[j].isSelect = 1;
                    }
                  }
                }
              }
            }
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.Message,
              showCancel: false
            })
          }
          that.setData({
            classList: classList
          })
        }
      })
    }

  },
  // 获取店铺行业
  getIndustryList: function () {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/store/GetIndustryList?storeId=' + app.globalData.storeId,
      success: function (res) {
        let classList = res.data.Data;
        if (res.data.Code == 0) {
          if (classList.length > 0) {
            for (let i = 0; i < classList.length; i++) {
              classList[i].isSelect = 0;
            }
          }
          that.getStaffExtend(classList);
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.getIndustryList();
    this.getAiCardInfo();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () { },
})